package com.zerosky.crm.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.ChanceDAO;
import com.zerosky.crm.po.Chance;
import com.zerosky.crm.service.ChanceService;
import com.zerosky.crm.util.PageInfo;

@Service(value="chanceService")
public class ChanceServiceImpl implements ChanceService {

	@Autowired
	private ChanceDAO chanceDao;

	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public List<Chance> getChanceList(Map<String, Object> map) {
		return chanceDao.getChanceList(map);
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<Chance> getPageInfoChance(Map<String, Object> map, PageInfo<Chance> pageInfo) {
		Integer limit = (pageInfo.getPageNo() - 1) * pageInfo.getPageSize();
		Integer pageSize = pageInfo.getPageSize();

		map.put("pageNo", limit);
		map.put("pageSize", pageSize);
		pageInfo.setPageCount(chanceDao.countChance(map));
		pageInfo.setList(chanceDao.getChanceList(map));
		return pageInfo;
	}

	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public Integer saveOrUpdate(Chance chance, String operation) {
		if (operation.equals("新增")) {
			return chanceDao.addChance(chance);
		} else if (operation.equals("修改")) {
			return chanceDao.modifyChance(chance);
		}
		return 0;
	}

	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public int deleteChance(String chanceId) {
		return chanceDao.deleteChanceById(chanceId);
	}

	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public Integer countChance(Map<String, Object> map) {
		return chanceDao.countChance(map);
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public Chance getChanceById(Map<String, Object> map) {
		return chanceDao.getChanceById(map);
	}
}
